﻿package com.mui.utils
{
    import com.gengine.global.*;
    import com.gengine.utils.pools.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.manager.*;
    import fl.core.*;
    import flash.display.*;
    import flash.utils.*;

    public class UICompomentPool extends Object
    {
        private static var pools:Dictionary = new Dictionary();
        private static var dicObj:Dictionary = new Dictionary(true);
        private static var _clientDisposeFun:Function;

        public function UICompomentPool()
        {
            return;
        }// end function

        public static function get clientDisposeFun() : Function
        {
            return _clientDisposeFun;
        }// end function

        public static function set clientDisposeFun(param1:Function) : void
        {
            _clientDisposeFun = param1;
            return;
        }// end function

        private static function getPool(param1:Class) : Array
        {
            return param1 in pools ? (pools[param1]) : (var _loc_2:* = new Array(), pools[param1] = new Array(), _loc_2);
        }// end function

        public static function getUICompoment(param1:Class, ... args)
        {
            var _loc_4:Object = null;
            args = getPool(param1);
            if (args.length > 0)
            {
                _loc_4 = args.pop();
                if (_loc_4 is IFrUIContainer)
                {
                    (_loc_4 as IFrUIContainer).createDisposedChildren();
                }
                if (_loc_4 is IFrUI)
                {
                    (_loc_4 as IFrUI).isDisposed = false;
                }
                return _loc_4;
            }
            else
            {
            }
            return construct(param1, args);
        }// end function

        public static function getUICompomentByParamsArray(param1:Class, param2:Array)
        {
            var _loc_4:Object = null;
            var _loc_3:* = getPool(param1);
            if (_loc_3.length > 0)
            {
                _loc_4 = _loc_3.pop();
                if (_loc_4 is IFrUIContainer)
                {
                    (_loc_4 as IFrUIContainer).createDisposedChildren();
                }
                if (_loc_4 is IFrUI)
                {
                    (_loc_4 as IFrUI).isDisposed = false;
                }
                return _loc_4;
            }
            else
            {
            }
            return construct(param1, param2);
        }// end function

        public static function disposeUICompoment(param1:DisplayObject, param2:Boolean = true, param3:Class = null) : Boolean
        {
            if (_clientDisposeFun != null)
            {
                _clientDisposeFun.call(null, param1);
            }
            if (!param1)
            {
                if (Global.isDebugModle)
                {
                    throw Error("disposeUIComponent  is  null");
                }
                return false;
            }
            if ((param1 as DisplayObject).parent)
            {
                (param1 as DisplayObject).parent.removeChild(param1 as DisplayObject);
            }
            if (param1 is UIComponent)
            {
                (param1 as UIComponent).clearStyle("textFormat");
                (param1 as UIComponent).clearStyle("disabledTextFormat");
            }
            param1.mask = null;
            param1.x = 0;
            param1.y = 0;
            param1["visible"] = true;
            param1["alpha"] = 1;
            param1["scaleX"] = 1;
            param1["scaleY"] = 1;
            param1["rotation"] = 0;
            param1["filters"] = [];
            if (param1.hasOwnProperty("enabled"))
            {
                param1["enabled"] = true;
            }
            if (param1 is Sprite)
            {
                (param1 as Sprite).graphics.clear();
            }
            if (param1 is IToolTipItem)
            {
                (param1 as IToolTipItem).toolTipData = null;
            }
            if (param2)
            {
                if (param1 is IFrUI)
                {
                    IFrUI(param1).isDisposed = true;
                }
                disposeObject(param1, param3);
            }
            return true;
        }// end function

        public static function disposeObject(param1, param2:Class = null) : void
        {
            var _loc_4:String = null;
            if (!param2)
            {
                _loc_4 = getQualifiedClassName(param1);
                param2 = getDefinitionByName(_loc_4) as Class;
            }
            var _loc_3:* = getPool(param2);
            if (_loc_3.indexOf(param1) == -1)
            {
                _loc_3.push(param1);
            }
            else if (Global.isDebugModle)
            {
                Alert.show(param1 + "两次放入对象池");
            }
            return;
        }// end function

        public static function clearPool() : void
        {
            pools = new Dictionary();
            return;
        }// end function

        public static function getSize() : int
        {
            var _loc_2:* = undefined;
            var _loc_1:int = 0;
            for each (_loc_2 in dicObj)
            {
                
                _loc_1++;
            }
            return _loc_1;
        }// end function

        public static function getPoolMsg() : String
        {
            var _loc_3:* = undefined;
            var _loc_4:Array = null;
            var _loc_1:String = "";
            var _loc_2:int = 0;
            for (_loc_3 in pools)
            {
                
                _loc_4 = getPool(_loc_3);
                _loc_2 = _loc_2 + _loc_4.length;
                _loc_1 = _loc_1 + (_loc_3 + ":<font color=\'#00FF00\'>" + _loc_4.length + "</font>\n");
            }
            return "对象池总对象数量：<font color=\'#00FF00\'>" + _loc_2 + "</font>\n" + _loc_1;
        }// end function

    }
}
